package com.cxf.windex.biz;

import com.cxf.windex.base.biz.BaseBiz;
import com.cxf.windex.base.common.ServiceException;
import com.cxf.windex.base.msg.ObjectRestResponse;
import com.cxf.windex.base.util.IdUtil;
import com.cxf.windex.entity.*;
import com.cxf.windex.mapper.JsFollowMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author chenxf
 * @date 3/15/2018
 * @description
 */
@Service
public class JsFollowBiz extends BaseBiz<JsFollowMapper, JsFollow> {
  @Autowired
  private BResourcesBiz bResourcesBiz;
  public String follow(String shopId,String openid,int type){
    if (StringUtils.isBlank(shopId)) {
      throw new ServiceException("请选择关注福娃");
    }
    BResouces shop=bResourcesBiz.selectById(Long.valueOf(shopId));
    if (shop == null) {
      throw new ServiceException("找不到ID为" + shopId + "的福娃");
    }

    JsFollow queryEntity = new JsFollow();
    queryEntity.setUserid(openid);
    queryEntity.setTouserid(shopId);
    JsFollow follow = selectOne(queryEntity);
    if (follow == null && type == 0) {
      throw new ServiceException("此福娃不在你的关注列表，无需取消关注");
    }
    if (follow != null && type == 1) {
      throw new ServiceException("此福娃已经在你的关注列表");
    }
    if (type == 0) {
      deleteById(follow.getId());
      return "取消成功";
    }
    queryEntity.setId(new IdUtil(4, 1).nextId() + "");
    insert(queryEntity);
    return "关注成功";
  }
  public List<BResouces> selectList(int limit, int offset,  String userid){
    PageHelper.startPage(offset, limit);
    return mapper.selectList(userid);
  }

}
